package com.yx.yinshanjinniu.activitys;

import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.adapters.TicketAdapter;
import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.TicketBean;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class TicketHistoryActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rg_group;
    private  String type = "2";
    private int page = 1;
    private SmartRefreshLayout refresh_layout;
    private RecyclerView rv_ticket_record;
    private List list = new ArrayList();
    private TicketAdapter adapter;
    private TextView not_data_tv;
    @Override
    protected int setContentView() {
        return R.layout.activity_ticket_history;
    }

    @Override
    protected void findViews() {
        rg_group = $(R.id.rg_group);
        not_data_tv = $(R.id.not_data_tv);
        refresh_layout = $ (R.id.refresh_layout);
        rv_ticket_record= $(R.id.ticket_history_rv);

        refresh_layout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        rv_ticket_record.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new TicketAdapter(mContext,list);
        rv_ticket_record.setAdapter(adapter);
        getListData();
    }

    @Override
    protected void setListeners() {
        rg_group.setOnCheckedChangeListener(this);
        refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getListData();
            }
        });

        refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getListData();
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_use_ticket:
                type = "2";
                page =1;
                getListData();
                break;
            case R.id.rb_expired_ticket:
                type = "3";
                page = 1;
                getListData();
                break;
        }
    }
    private void getListData(){

        refresh_layout.finishRefresh(2000);
        refresh_layout.finishLoadMore(2000);
        mAppAction =  MyApplication.getApplication().getAppAction();
        mAppAction.getUserTicketList(type,page, new ActionCallbackListener<List<TicketBean>>() {
            @Override
            public void onSuccess(List<TicketBean> data) {
                if (page == 1){
                    list.clear();
                }
                list.addAll(data);
                if(list.size()>0){
                    not_data_tv.setVisibility(View.GONE);
                    rv_ticket_record.setVisibility(View.VISIBLE);
                }else{
                    not_data_tv.setVisibility(View.VISIBLE);
                    rv_ticket_record.setVisibility(View.GONE);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });

    }
}
